﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class GraphicNode : MonoBehaviour {
    public GraphicNode lastNode;

    public enum NodeType
    {
        Normal,
        Start,
        End,
        Obstacle
    }
    public NodeType type;

    public int hValue;
    public int gValue;

    public Text indexText;
    public Text hValueText;
    public Text gValueText;
    public Text fValueText;

    public Color color
    {
        set
        {
            GetComponent<Image>().color = value;
        }
        get
        {
            return GetComponent<Image>().color;
        }
    }

    public int fValue
    {
        get { return hValue + gValue; }
    }

    public int x, y;

    public void SetIndex(int _x, int _y)
    {
        x = _x;
        y = _y;
        indexText.text = "(" + x + "," + y + ")";
    }

    public void UpdateText()
    {
        if(type == NodeType.Normal)
        {
            //gValue = (int)(Mathf.Sqrt(Mathf.Abs(x - startNode.x) * Mathf.Abs(x - startNode.x) + Mathf.Abs(y - startNode.y) * Mathf.Abs(y - startNode.y)) * 10);
            //hValue = (Mathf.Abs(x - endNode.x) + Mathf.Abs(y - endNode.y)) * 10;
            gValueText.text = "g:" + gValue;
            hValueText.text = "h:" + hValue;
            fValueText.text = fValue.ToString();
        }
    }

    public override string ToString()
    {
        return "(" + x + "," + y + ")";
    }

    [ContextMenu("Set as Obstacle")]
    void SetAsObastacle()
    {
        type = NodeType.Obstacle;
        GetComponent<Image>().color = Color.blue;
    }

    [ContextMenu("Set as Normal")]
    void SetAsNormal()
    {
        type = NodeType.Normal;
        GetComponent<Image>().color = Color.white;
    }

    [ContextMenu("Set as Start")]
    void SetAsStart()
    {
        type = NodeType.Start;
        GetComponent<Image>().color = Color.red;
    }

    [ContextMenu("Set as End")]
    void SetAsEnd()
    {
        type = NodeType.End;
        GetComponent<Image>().color = Color.green;
    }
}
